import net from '../dxmodules/dxNet.js'
import common from '../dxmodules/dxCommon.js'
import logger from '../dxmodules/dxLogger.js'
import std from '../dxmodules/dxStd.js'
let type = 1 //Network type (required)
//For obtaining an IP address via dynamic or static methods, net.DHCP.STATIC is required for static IP address acquisition.
let dhcp = net.DHCP.DYNAMIC
let ip = "192.168.60.111" //Static fields are required
let gateway = "192.168.60.1"//Static fields are required
let subnetMask = "255.255.255.0"//Static fields are required
let dns = "218.4.4.4,218.2.2.2".split(",")//Static fields are required
let macAddr = common.getUuid2mac()//Get device MAC address
let options = {
    type: type,
    dhcp: dhcp,
    ip: ip,
    gateway: gateway,
    netmask: subnetMask,
    dns0: dns[0],
    dns1: dns[1],
    macAddr: macAddr
}
//Initialize the network
net.init(options)

//Loop to obtain network status reports
std.setInterval(() => {
    if (!net.msgIsEmpty()) {
        let res = net.msgReceive();
        //res.status >= 4 represents obtaining an IP address and connecting to the internet.
        if (res.status >= 4) {
            res.connected = true
        } else {
            res.connected = false
        }
    }
}, 20)


//Loop to get network status
 std.setInterval(()=>{
    logger.info('Get network status: ',JSON.stringify(net.getStatus()));
 },2000)

